package com.yuanfeng.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.goods.entity.GoodsArticleRemarkEntity;
import com.yuanfeng.goods.mapper.GoodsArticleInfoMapper;
import com.yuanfeng.goods.mapper.GoodsArticleRemarkMapper;
import com.yuanfeng.goods.service.GoodsArticleRemarkService;
import com.yuanfeng.goods.vo.CommunityCommentsDetailsVO;
import com.yuanfeng.goods.vo.CommunityCommentsVO;
import com.yuanfeng.goods.vo.CommunityRemarkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class GoodsArticleRemarkServiceImpl extends ServiceImpl<GoodsArticleRemarkMapper, GoodsArticleRemarkEntity>
    implements GoodsArticleRemarkService{

    @Autowired
    private GoodsArticleInfoMapper goodsArticleInfoMapper;
    @Override
    public ResponseResult updateStatus(Map<Object, Object> map) {
        this.baseMapper.updateCommunityCommentStatus(map);

        if ( Integer.parseInt(map.get("auditState").toString())==1){
            goodsArticleInfoMapper.updateRemarkNum(map);
        }
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);

    }

    @Override
    public ResponseResult selectDetailsList(Map<Object, Integer> map) {
        Integer remarkId = map.get("remarkId");
        List<CommunityCommentsDetailsVO> communityCommentsDetailsParams = this.baseMapper.selectDetailsList(remarkId);
        return ResponseResult.success(communityCommentsDetailsParams);
    }

    @Override
    public ResponseResult selectList(Map<String, Object> map) {
        HashMap<Object, Object> ob = new HashMap<>();
        IPage<CommunityCommentsVO> page=this.baseMapper.selectCommunityComments(new Query<CommunityCommentsVO>().getPage(map),
                map);

        if(page.getRecords() !=null && page.getRecords().size()>0){
            for (CommunityCommentsVO communityCommentsParam :  page.getRecords()) {
                System.out.println(communityCommentsParam);
                if (communityCommentsParam.getRemarkedUserNickName() == null ){
                    communityCommentsParam.setRemarkedUserNickName(communityCommentsParam.getRemarkedUserPhone());
                }
                if (communityCommentsParam.getRemarkUserNickName() == null){
                    communityCommentsParam.setRemarkUserNickName(communityCommentsParam.getRemarkUserPhone());
                }
            }
        }

        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult queryRemarkList(Map<String, Object> param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        if(null != userInfo){
            param.put("userId",userInfo.getUserId());
            IPage<CommunityRemarkVO> page = this.baseMapper.getRemarkList(new Query<CommunityRemarkVO>().getPage(param),param);
            List<CommunityRemarkVO> records = page.getRecords();
            for(CommunityRemarkVO remark:records){
                String remarkedContent = null;
                Map<String,String> remarkUserInfo =   this.baseMapper.getUserInfo(remark.getUserId());
                remark.setUserName(remarkUserInfo.get("userName"));
                remark.setUserAvatar(remarkUserInfo.get("userAvatar"));
                Integer remarkType = remark.getRemarkType();
                //对心得的评论
                if(1 == remarkType){
                    remarkedContent = goodsArticleInfoMapper.getArticleContent(remark.getRemarkedId());
                }else if(2 == remarkType){
                    //对评论的评论
                    remarkedContent = this.baseMapper.getRemarkContent(remark.getRemarkedId());
                }
                remark.setRemarkedContent(remarkedContent);
            }
            this.baseMapper.update(null,new LambdaUpdateWrapper<GoodsArticleRemarkEntity>()
                    .set(GoodsArticleRemarkEntity::getIsRead, CommonType.commonStatus.YES.getCode())
                    .eq(GoodsArticleRemarkEntity::getRemarkedUser,userInfo.getUserId()));
            page.setRecords(records);
            return ResponseResult.success(PageUtils.getPage(page));
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
    }

    @Override
    public ResponseResult addRemark(Map<Object, Object> param) {
        UserInfoVO ui = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        if(null == ui){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }
        param.put("remarkUser", ui.getUserId());
        if(param.get("remarkUser").equals(param.get("remarkedUser"))){
            param.put("isRead", CommonType.commonStatus.YES.getCode());
        }else {
            param.put("isRead", CommonType.commonStatus.NO.getCode());
        }
        this.baseMapper.addRemark(param);
        return ResponseResult.success();
    }
}




